﻿#include "supply_dialog.h"
#include "ui_supply_dialog.h"
#include <QLabel>
#include <QMessageBox>
#include "basal_sql.h"
#include <QValidator>
#include <QRegExp>
//供应商信息界面
supply_dialog::supply_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::supply_dialog)
{
    ui->setupUi(this);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->editSName->setFocus();

    QIntValidator *validator = new QIntValidator(this);
    ui->editCell->setValidator(validator);


    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //取消水平滚动条

}

supply_dialog::~supply_dialog()
{
    delete ui;
}

//添加供应商按钮
void supply_dialog::on_addSupBtn_clicked()
{
    QString sName = ui->editSName->text();
    QString sAdmin = ui->editAdmin->text();
    QString sAddr = ui->editAdr->text();
    QString sCell = ui->editCell->text();
    QString sNote = ui->tEditNote->toPlainText();

    if(sName.isEmpty())
        {
            ui->errorLable->setText("请输入供应商名称!");
            ui->editSName->setFocus();
            return;
        }
        else if(sAdmin.isEmpty())
        {
            ui->errorLable->setText("请输入联系人!");
            ui->editAdmin->setFocus();
            return;
        }
        else if(sAddr.isEmpty())
        {
            ui->errorLable->setText("请输入厂家地址!");
            ui->editAdr->setFocus();
            return;
        }
        else if(sCell.isEmpty())
        {
            ui->errorLable->setText("请输入联系电话!");
            ui->editCell->setFocus();
            return;
        }


    QSqlQuery query;
    //判断厂商是否存在
    query.exec(tr("select s_name from supply"));
    bool hasSupplier = false;
    while(query.next())
        {
            if(query.value(0).toString() == ui->editSName->text())
            {
                hasSupplier = true;
                break;
            }
        }
    if(hasSupplier) //如果已经有了该供应商
        {
            int r = QMessageBox::question(this, tr("警告"), tr("该供应商已经存在，是否强制添加？"), QMessageBox::No | QMessageBox::Yes);
            if(r == QMessageBox::No)
            {
                ui->editSName->selectAll();
                ui->editSName->setFocus();
                return;
            }
        }
    QString sql = "insert into supply(s_name,s_admin,s_cell,s_adress,s_note) "
                  "values('"+sName+"','"+sAdmin+"','"+sCell+"','"+sAddr+"','"+sNote+"')";
    if(query.exec(sql)){
        QMessageBox msgBox;
        msgBox.setText("添加成功！");
        while(ui->tableWidget->rowCount()){
            ui->tableWidget->removeRow(0);
        }
        supply_dialog::showEvent(NULL);
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                msgBox.setWindowTitle(tr("提示"));
                msgBox.setButtonText(QMessageBox::Yes, tr("继续添加下一家"));
                msgBox.setButtonText(QMessageBox::No, tr("关闭"));
                msgBox.setDefaultButton(QMessageBox::No);
                int ret = msgBox.exec();
                if(ret == QMessageBox::Yes)     //继续添加下一条
                {
                    ui->editSName->clear();
                    ui->editAdmin->clear();
                    ui->editAdr->clear();
                    ui->editCell->clear();
                    ui->tEditNote->clear();
                    ui->editSName->setFocus();
                }
                else
                {
                    this->accept();
                }
    }


}

//初始化界面显示所有供应商的信息
void supply_dialog::showEvent(QShowEvent *event)
{
    sqlite3 *pDb;
    if(sqlite3_open("./supermarket.db", &pDb)==SQLITE_OK){
        QString sql="select * from supply order by s_id DESC;";
        sqlite3_stmt *pStmt;
        if(sqlite3_prepare(pDb, sql.toStdString().c_str(), sql.length(), &pStmt, NULL)==SQLITE_OK){
            while(sqlite3_step(pStmt) == SQLITE_ROW){
                const unsigned char *pId = sqlite3_column_text(pStmt, 0);
                const unsigned char *pName = sqlite3_column_text(pStmt, 1);
                const unsigned char *pAdmin = sqlite3_column_text(pStmt, 2);
                const unsigned char *pAddress = sqlite3_column_text(pStmt, 3);
                const unsigned char *pCell = sqlite3_column_text(pStmt, 4);
                const unsigned char *pNote = sqlite3_column_text(pStmt, 5);

                QTableWidgetItem *pIdItem = new QTableWidgetItem((char *)pId);
                QTableWidgetItem *pNameItem = new QTableWidgetItem((char *)pName);
                QTableWidgetItem *pAdminItem = new QTableWidgetItem((char *)pAdmin);
                QTableWidgetItem *pAddressItem = new QTableWidgetItem((char *)pAddress);
                QTableWidgetItem *pCellItem = new QTableWidgetItem((char *)pCell);
                QTableWidgetItem *pNoteItem = new QTableWidgetItem((char *)pNote);

                ui->tableWidget->insertRow(0);

                ui->tableWidget->setItem(0, 0, pIdItem);
                ui->tableWidget->setItem(0, 1, pNameItem);
                ui->tableWidget->setItem(0, 2, pAdminItem);
                ui->tableWidget->setItem(0, 3, pAddressItem);
                ui->tableWidget->setItem(0, 4, pCellItem);
                ui->tableWidget->setItem(0, 5, pNoteItem);
            }
        }
    }
}

//删除供应商的信息（操作数据库表）（之前那种写法太复杂，还是这样直接使用QSqlQuery方便）
void supply_dialog::on_delSupBtn_clicked()
{
    int row = ui->tableWidget->currentRow();
    if(row >= 0) {
        QTableWidgetItem *sIdItem=ui->tableWidget->item(row, 0);
        QString id = sIdItem->text();

        QSqlQuery query;
        QString sql = "delete from supply where s_id='" +id+ "';";
        if(query.exec(sql)){
            ui->tableWidget->removeRow(row);
            QMessageBox::about(NULL, "提示", "删除成功");
        }
    }
}
